/**
 * $Id: Parser.java 20 2006-09-16 20:32:08Z willi.w $
 */
package at.wwu.tunes2db;

import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;

import at.wwu.tunes2db.xml.ConfigurationHandler;

/**
 * @author willi.w
 * open the config.xml file, set the handler, parse it
 */
public class Configuration {
    XMLReader reader = null;

    ContentHandler handler = null;

    InputSource source = null;

    File conf = null;

    InputStreamReader filestreamreader = null;

    public Configuration(ConfigurationHandler h) throws SAXException, FileNotFoundException {
        reader = XMLReaderFactory.createXMLReader();
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        InputStream config = this.getClass().getClassLoader().getResourceAsStream(
        "tunes2db_conf.xml");
        if (config==null) {
            throw new FileNotFoundException();
        }
        try {
            filestreamreader = new InputStreamReader(config, "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        handler = h;
        source = new InputSource(filestreamreader);
    }

    public void parse() throws IOException, SAXException {
        reader.setContentHandler(handler);
        reader.parse(source);
        reader.toString();
    }
}
